import axios from "axios";
const url = 'http://www.yanshisites.top/api';
import { getUserInfo } from '../util';
import { message } from 'antd';

export const request = (config: any) => {
  const { session_key } = getUserInfo();
  return new Promise((resolve, reject) => {
    axios({
      url,
      ...config,
      method: config.method || 'get',
      data: {
        session_key,
        ...config.data
      },
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }).catch(function (error) {
      message.error(error);
      reject(error);
    })
      .then((res: any) => {
        if (res.data.code === 1) {
          resolve(res.data);
        }
        else if (res.data.code === -100) {
          window.location.href = '/';
        }
        else {
          message.error(res.data.msg);
          reject(res.data.msg);
        }
      })
  })
}